package com.qinhailin.portal.dbm.ctrl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.qinhailin.common.base.BaseController;
import com.qinhailin.common.routes.ControllerBind;
import com.qinhailin.common.util.AESUtil;
import com.qinhailin.common.util.DateUtil;
import com.qinhailin.portal.dbm.model.CTCustomer;
import com.qinhailin.portal.dbm.model.DTCustomer;
import com.qinhailin.portal.dbm.service.CTCustomerServiceImpl;
import com.qinhailin.portal.dbm.service.DPListtaskServiceImpl;
import com.qinhailin.portal.dbm.service.DTContactServiceImpl;
import com.qinhailin.portal.dbm.service.DTCustomerServiceImpl;
import com.qinhailin.portal.dbm.service.DTSystemdataServiceImpl;
import com.qinhailin.portal.dbm.service.TimedService;

/**
 * 联系人
 * @author ward
 */
@ControllerBind(path="/portal/dbm/heartbeat")
@Clear
public class HeartbeatController extends BaseController{
	
	private static TimedService timedService = TimedService.getInstance();
	
	//@Inject
	//CTCustomerServiceImpl service;
	//@Inject
	//CPGetIDService idService;
	//@Inject
	//DTSystemdataServiceImpl systemdataService;
	
	@Inject
	DTContactServiceImpl service;
	private static final Logger log = Logger.getLogger(HeartbeatController.class);
	
	@Inject
	private DPListtaskServiceImpl listtaskService;
	@Inject
	private CTCustomerServiceImpl ccustomerService;
	@Inject
	private DTCustomerServiceImpl dcustomerService;
	//@Inject
	//private DTSystemdataServiceImpl systemdataService;
	
	public void index() {
		Timestamp now = DateUtil.getCurrentTimestamp();
		Integer rCustID = this.getInt();
		//CTCustomer cc = this.getSessionAttr("customer");
		CTCustomer cc = ccustomerService.findByID(rCustID);
		DTCustomer dc = dcustomerService.findByID(rCustID);
		if(dc != null && ((dc.getXNextHeartbeatTime() != null && dc.getXNextHeartbeatTime().after(now)) || (dc.getXNextCmdTime() != null && dc.getXNextCmdTime().after(now)))) {
			log.warn("HEARBEAT rCustID:" + rCustID + ", INTERVAL TOO SHORT");
			this.renderJson(AESUtil.encrypt(JSONArray.toJSONString(new ArrayList<Integer>()), cc.getStr("xKey")));
			return;
		}
		// 差点忘记了加密
		List<Integer> tasks = listtaskService.dp_listtask(rCustID);
		String json = JSONArray.toJSONString(tasks);
		String rs = AESUtil.encrypt(json, cc.getXKey());
		Timestamp current = DateUtil.getCurrentTimestamp();
		log.info("HEARBEAT rCustID:" + rCustID + ", COST:" + (current.getTime() - now.getTime()) + ", RESULT:" + json);
		Timestamp next = new Timestamp(current.getTime() + ccustomerService.heartbeatInterval(rCustID));
		dc.setXLastHeartbeatTime(now).setXNextHeartbeatTime(next);
		if(tasks != null && tasks.size() > 0) {
			dc.setXLastCmd(tasks != null && tasks.size() > 0 ? json : "")
			  .setXLastCmdTime(tasks != null && tasks.size() > 0 ? current : null)
			  .setXNextCmdTime(next);
		}
		dcustomerService.update(dc);
		this.renderJson(rs);
	}
		

}
